package cn.android.weblog;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;

import cn.android.weblog.api.UserAPI;

public class ChangePasswordActivity extends AppCompatActivity {

    private Button change;
    private TextView back;
    private EditText pass,nPass,repeatPass;
    private UserAPI userAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        change=(Button)findViewById(R.id.change_button);
        back=(TextView)findViewById(R.id.change_back);
        pass=(EditText)findViewById(R.id.change_pass);
        nPass=(EditText)findViewById(R.id.change_new_pass);
        repeatPass=(EditText)findViewById(R.id.change_repeat);
        userAPI=new UserAPI();

        // 修改密码 t
        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePass();
            }
        });

        // 返回按钮 t
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ChangePasswordActivity.this,MyActivity.class);
                startActivity(intent);
            }
        });

    }

    /**
     * 修改密码 t
     */
    public void changePass(){
        String password=pass.getText().toString().trim();
        String newPass=nPass.getText().toString().trim();
        String rePass=repeatPass.getText().toString().trim();
        if (newPass.equals("") || rePass.equals("") || password.equals("")) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        if (!newPass.equals(rePass)) {
            Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_LONG).show();
            return;
        }
        Map<String, Object> map = userAPI.changePassword(password, newPass);
        if(map.get("Msg") == null) {
            Toast.makeText(this, "修改成功", Toast.LENGTH_LONG).show();
            // 是否要页面跳转
            //Intent intent=new Intent(ChangePasswordActivity.this,MyActivity.class);
            //startActivity(intent);
        }else{
            Toast.makeText(this, map.get("Msg").toString(), Toast.LENGTH_LONG).show();
        }
    }

}
